package com.example.doer.reading;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.view.sliding.AbSlidingPlayView;

import static com.ab.util.AbToastUtil.showToast;


public class BookStoreFragment extends Fragment {
    AbSlidingPlayView abSlidingPlayView=null;

    private View view;
    public BookStoreFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        this.view=inflater.inflate(R.layout.fragment_book_store, container, false);
        addSlidingView(inflater,container);
        setLayout();


        return view;
    }

    public void addSlidingView(LayoutInflater inflater,ViewGroup container){
        abSlidingPlayView = (AbSlidingPlayView)view.findViewById(R.id.mAbSlidingPlayView);


        final View mPlayView = inflater.inflate(R.layout.play_view_item, container,false);
        ImageView mPlayImage = (ImageView) mPlayView.findViewById(R.id.play_View);
        TextView mPlayText = (TextView) mPlayView.findViewById(R.id.play_text);
        mPlayText.setText("这是第一幅图");
        mPlayImage.setBackgroundResource(R.drawable.playimage1);

        final View mPlayView1 = inflater.inflate(R.layout.play_view_item, container,false);
        ImageView mPlayImage1 = (ImageView) mPlayView1.findViewById(R.id.play_View);
        TextView mPlayText1 = (TextView) mPlayView1.findViewById(R.id.play_text);
        mPlayText1.setText("这是第二幅图");
        mPlayImage1.setBackgroundResource(R.drawable.playimage2);

        final View mPlayView2 = inflater.inflate(R.layout.play_view_item, container,false);
        ImageView mPlayImage2 = (ImageView) mPlayView2.findViewById(R.id.play_View);
        TextView mPlayText2 = (TextView) mPlayView2.findViewById(R.id.play_text);
        mPlayText2.setText("这是第三幅图");
        mPlayImage2.setBackgroundResource(R.drawable.playimage3);
        abSlidingPlayView.setNavPageResources(R.drawable.play_display,R.drawable.play_hide);
        abSlidingPlayView.setNavHorizontalGravity(Gravity.RIGHT);
        abSlidingPlayView.addView(mPlayView);
        abSlidingPlayView.addView(mPlayView1);
        abSlidingPlayView.addView(mPlayView2);
        abSlidingPlayView.setOnItemClickListener(new AbSlidingPlayView.AbOnItemClickListener() {

            @Override
            public void onClick(int position) {
                showToast(getActivity(),"点击"+position);
            }
        });

        abSlidingPlayView.setOnPageChangeListener(new AbSlidingPlayView.AbOnChangeListener() {

            @Override
            public void onChange(int position) {
                //当fragment生命周期结束并销毁时，getActivity()返回的会是null。
                //所以应该定义一个全局的context=getActivity();供整个fragment调用
                //if(getActivity()!=null)
                //if(BookStoreFragment.this!=null&&BookStoreFragment.this.isAdded())
                //showToast(getActivity(),"改变"+position);
            }
        });
        abSlidingPlayView.startPlay();
    }

    public void setLayout(){
        RelativeLayout layout1=(RelativeLayout)this.view.findViewById(R.id.Store_Layout1);
        ImageView imageView1=(ImageView)layout1.findViewById(R.id.imageView);
        imageView1.setImageResource(R.drawable.ic_launcher);
        layout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast(getContext(),"分类");
            }
        });
        RelativeLayout layout2=(RelativeLayout)this.view.findViewById(R.id.Store_Layout2);
        ImageView imageView2=(ImageView)layout2.findViewById(R.id.imageView2);
        imageView2.setImageResource(R.drawable.ic_launcher);
        layout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast(getContext(),"畅销");
            }
        });
        RelativeLayout layout3=(RelativeLayout)this.view.findViewById(R.id.Store_Layout3);
        ImageView imageView3=(ImageView)layout3.findViewById(R.id.imageView3);
        imageView3.setImageResource(R.drawable.ic_launcher);
        layout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast(getContext(),"新书");
            }
        });
        RelativeLayout layout4=(RelativeLayout)this.view.findViewById(R.id.Store_Layout4);
        ImageView imageView4=(ImageView)layout4.findViewById(R.id.imageView4);
        imageView4.setImageResource(R.drawable.ic_launcher);
        layout4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast(getContext(),"好评");
            }
        });
    }
}
